﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using SensitivityTest.model;
using System.Net;
using System.Net.Sockets;
using SensitivityTest.ValidProductTypeID;

namespace SensitivityTest
{
    public partial class Frm_Config_Add : Form
    {
        private Test_Record_OIP3_DataUploadAndProcessControl_ServiceSoapClient validScodeAndUpload;
        MDIParent1 M = null;
        public Frm_Config_Add(MDIParent1 m)
        {
            InitializeComponent();
            M = m;
        }

        private void btn_reset_Click(object sender, EventArgs e)
        {
            clearControlData();
        }
        /// <summary>
        /// 添加配置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_addconfig_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txt_productName.Text.Trim()) || string.IsNullOrEmpty(txt_sig1Freq.Text.Trim()) || string.IsNullOrEmpty(txt_sig2Freq.Text.Trim()) || string.IsNullOrEmpty(txt_centerFreq.Text.Trim()) || string.IsNullOrEmpty(txt_span.Text.Trim()) || string.IsNullOrEmpty(txt_RBW.Text.Trim()) || string.IsNullOrEmpty(txt_VBW.Text.Trim()) || string.IsNullOrEmpty(txt_REF.Text.Trim()) || string.IsNullOrEmpty(txt_Atten.Text.Trim()) || string.IsNullOrEmpty(txt_Enable.Text.Trim()) || string.IsNullOrEmpty(txt_spec.Text.Trim()) || string.IsNullOrEmpty(txt_ChanList.Text.Trim()))
            {
                MessageBox.Show("配置信息不能为空");
                return;
            }
            if (CheckProductTypeID() == false)                                   //  S代码校验
            {
                txt_productName.Focus();
                txt_productName.SelectAll();
                return;
            }
            int rowscount = SQLData.SelectScodeForConfig("select count(ProductTypeID) from Test_OIP3_Config where ProductTypeID= '" + txt_productName.Text + "' and Versions='" + txt_Versions.Text.Trim() + "'");

            if (rowscount>0)
            {
                MessageBox.Show("该产品已经存在当前版本配置");
                return;
            }

            try
            {
                SQLData.insertConfigData("insert into Test_OIP3_Config(ProductTypeID,F1,F2,CenterFreq,span,RBW,VBW,REF,Atten,Versions,Spec,Enable,ChanList) values('" + txt_productName.Text.Trim() + "','" + txt_sig1Freq.Text.Trim() + "','" + txt_sig2Freq.Text.Trim() + "','" + txt_centerFreq.Text.Trim() + "','" + txt_span.Text.Trim() + "','" + txt_RBW.Text.Trim() + "','" + txt_VBW.Text.Trim() + "','" + txt_REF.Text.Trim() + "','" + txt_Atten.Text.Trim() + "','" + txt_Versions.Text.Trim() + "','" + txt_spec.Text.Trim() + "','" + txt_Enable.Text.Trim() + "','" + txt_ChanList.Text.Trim() + "')");
                MessageBox.Show("数据保存成功");
                this.Close();
            }
            catch
            {
                MessageBox.Show("数据保存失败");
                return;
            }
           

        }

        private void clearControlData()
        {
            txt_productName.Text= "";
            txt_sig1Freq.Text = "";
            txt_sig2Freq.Text = "";
            txt_centerFreq.Text = "";
            txt_span.Text = "";
            txt_RBW.Text = "";
            txt_VBW.Text = "";
            txt_REF.Text = "";
            txt_Atten.Text = "";
            txt_Versions.Text = "";
            txt_Enable.Text = "";
            txt_ChanList.Text = "";
            txt_spec.Text = "";
        }
        /// <summary>
        /// 关闭窗体时，设置父窗体按钮enabke
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Frm_Config_Add_FormClosing(object sender, FormClosingEventArgs e)
        {
            //this.DialogResult = DialogResult.OK;
            M.添加配置ToolStripMenuItem.Enabled = true;
        }

        private void Frm_Config_Add_Load(object sender, EventArgs e)
        {
            GetLocalIP();
        }

        private void GetLocalIP()
        {
            string NetSeg =MDIParent1.localIP.Split('.')[2];
            if (int.Parse(NetSeg) >= 80 && int.Parse(NetSeg) <= 89)
            {
                SQLData.mServer = "10.53.80.11";
            }
            else if (int.Parse(NetSeg) >= 60 && int.Parse(NetSeg) <= 69)
            {
                SQLData.mServer = "10.53.60.6";
            }
            else
            {
                SQLData.mServer = "10.53.20.170";
            }
        }

        public bool CheckProductTypeID()
        {
            validScodeAndUpload = new Test_Record_OIP3_DataUploadAndProcessControl_ServiceSoapClient("Test_Record_OIP3_DataUploadAndProcessControl_ServiceSoap", MDIParent1.ScodeAndUpload);      //S代码校验
            if (!validScodeAndUpload.IsValidProductTypeID(txt_productName.Text))
            {
                MessageBox.Show("S代码错误");
                return false;
            }
            return true;
        }
    }
}
